package com.demo.util.excle;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;

public class ImportListener<T> extends AnalysisEventListener<T> {
    private int batchCount = 3000;
    private List<T> list = new ArrayList();
    private final ExcelImporter<T> importer;

    public void invoke(T data, AnalysisContext analysisContext) {
        this.list.add(data);
        if (this.list.size() >= this.batchCount) {
            this.importer.save(this.list);
            this.list.clear();
        }

    }

    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        this.importer.save(this.list);
        this.list.clear();
    }

    public int getBatchCount() {
        return this.batchCount;
    }

    public List<T> getList() {
        return this.list;
    }

    public ExcelImporter<T> getImporter() {
        return this.importer;
    }

    public void setBatchCount(final int batchCount) {
        this.batchCount = batchCount;
    }

    public void setList(final List<T> list) {
        this.list = list;
    }

    public String toString() {
        return "ImportListener(batchCount=" + this.getBatchCount() + ", list=" + this.getList() + ", importer=" + this.getImporter() + ")";
    }

    public ImportListener(final ExcelImporter<T> importer) {
        this.importer = importer;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof ImportListener)) {
            return false;
        } else {
            ImportListener<?> other = (ImportListener)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (!super.equals(o)) {
                return false;
            } else if (this.getBatchCount() != other.getBatchCount()) {
                return false;
            } else {
                label40: {
                    Object this$list = this.getList();
                    Object other$list = other.getList();
                    if (this$list == null) {
                        if (other$list == null) {
                            break label40;
                        }
                    } else if (this$list.equals(other$list)) {
                        break label40;
                    }

                    return false;
                }

                Object this$importer = this.getImporter();
                Object other$importer = other.getImporter();
                if (this$importer == null) {
                    if (other$importer != null) {
                        return false;
                    }
                } else if (!this$importer.equals(other$importer)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof ImportListener;
    }

    public int hashCode() {
        //int PRIME = true;
        int result = super.hashCode();
        result = result * 59 + this.getBatchCount();
        Object $list = this.getList();
        result = result * 59 + ($list == null ? 43 : $list.hashCode());
        Object $importer = this.getImporter();
        result = result * 59 + ($importer == null ? 43 : $importer.hashCode());
        return result;
    }
}
